﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using mshtml;
using WebHacker;
using System.Web;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]	
namespace EccHttpLib
{
    public class CheckResult

    {
        //alert(document.all.idName.value);
        //alert(document.all.idNo.value);
        //alert(document.all.idAdd.value);
        //alert(document.all.idTel.value);
        //alert(document.all.idAdd2.value);
        //alert(document.all.idTel2.value);
        public string idName { get; set; }
        public string idNo { get; set; }
        public string idAdd { get; set; }
        public string idTel { get; set; }
        public string idAdd2 { get; set; }
        public string idTel2 { get; set; }

        //
        public string idName0 { get; set; }
        public string idNo0 { get; set; }
        //
        public string idName1 { get; set; }
        public string idNo1 { get; set; }
        public string strCopy1 { get; set; }
        public string idName2 { get; set; }
        public string idNo2 { get; set; }
        public string strCopy2 { get; set; }
        public string idName3 { get; set; }
        public string idNo3 { get; set; }
        public string strCopy3 { get; set; }

        public string idMatch { get; set; }

    }

    //public class RequestForm : XMLHttpForm
    //{
    //    public 
    //}

    public class OnlineCheck
    {
        public static void hehe()
        {
            logger.Info("hehe");
        }
        public OnlineCheck(object obj)
        {
            
            _DomDocment = obj as HTMLDocument;
        }

        public bool CheckInfo(string str)
        {
            _DomDocment.getElementById("idInput").setAttribute("value",str);

            var v1 =
                _DomDocment.scripts;//??hehe

            string strScript = "fnSelect();";
            foreach (var VARIABLE in v1)
            {
                if((VARIABLE as HTMLScriptElement).src == null)
                {
                    var res =
                         _DomDocment.parentWindow.execScript(strScript);
                    return true;
                }

            }
            return false;
        }

        public CheckResult GetProfile()
        {
            CheckResult cr = new CheckResult();

            //+Menu
            cr.idName = Utility.GetAttributeById(_DomDocment,"idName","value");

            cr.idNo = Utility.GetAttributeById(_DomDocment, "idNo", "value");

            cr.idAdd = Utility.GetAttributeById(_DomDocment, "idAdd", "value");

            cr.idTel = Utility.GetAttributeById(_DomDocment, "idTel", "value");

            cr.idAdd2 = Utility.GetAttributeById(_DomDocment, "idAdd2", "value");

            cr.idTel2 = Utility.GetAttributeById(_DomDocment, "idTel2", "value");
            //-Menu

            string[] profileItems;

            profileItems =
            this.getItems(HttpUtility.UrlEncode("e-mail") + "=" + HttpUtility.UrlEncode(cr.idAdd));

            cr.idName2 = profileItems[0];
            cr.idNo2 = profileItems[1].Split('　')[0];
            cr.strCopy2 = profileItems[2];

            logger.Info("l1-1");
            profileItems =
            this.getItems(HttpUtility.UrlEncode("custno") + "=" + HttpUtility.UrlEncode(cr.idNo));
            logger.Info("l1-2");
            cr.idName1 = profileItems[0];
            cr.idNo1 = profileItems[1].Split('　')[0];
            cr.strCopy1 = profileItems[2];

            if (!"".Equals(cr.idAdd2))
            {
                profileItems =
                this.getItems(HttpUtility.UrlEncode("e-mail") + "=" + HttpUtility.UrlEncode(cr.idAdd2));

                cr.idName3 = profileItems[0];
                cr.idNo3 = profileItems[1].Split('　')[0];
                cr.strCopy3 = profileItems[2];
            }
            return cr;
        }

        private static ILog logger = LogManager.Exists("Test");

        public void test()
        {
            HTMLTextAreaElementClass _html =
                _DomDocment.getElementById("idInput") as HTMLTextAreaElementClass;
            HTMLInputElementClass _htmlInputElementClass =
                _html.nextSibling as HTMLInputElementClass;
            IHTMLElement _htmlElementClass = _htmlInputElementClass as IHTMLElement;

        }

        private string[] gOnlineKey = new string[] { "顧客姓名(漢字)", "顧客番号" };
        private string[] getItems(string para)
        {
            string[] res = new string[3];
            HTMLDocument _htmlDocument = null;

            WebTools wt = new WebTools();
            _htmlDocument =
            wt.SubmitFormData("http://svfile05/scripts/Cust_Inqiry/kensaku3.asp",
                    para);


            logger.Info("Para : " + para);
            logger.Info("_htmlDocument.body.outerHtml : " + _htmlDocument.body.outerHTML);

            int sIdx =
                _htmlDocument.body.outerHTML.IndexOf("clipboardData.setData('Text','");

            logger.Info("sIdx : " + sIdx);
            if(sIdx != -1)
            {
                res[2] = _htmlDocument.body.outerHTML.Substring(sIdx);
                logger.Info("res[2] : " + res[2]);

                sIdx = res[2].IndexOf("');alert('");
                if (sIdx != -1)
                {
                    logger.Info("sIdx : " + sIdx);
                    res[2] = res[2].Substring(0, sIdx).Replace("clipboardData.setData('Text','", "").Replace("\\n", "\n");
                    logger.Info("res[2] : " + res[2]);
                }

            }


            HTMLTableCell _htmlHtmlElementClass;
            bool flg = false;
            foreach (var VARIABLE in _htmlDocument.getElementsByTagName("TH"))
            {
                _htmlHtmlElementClass =
                    VARIABLE as HTMLTableCell;

                if (_htmlHtmlElementClass.innerText.IndexOf(gOnlineKey[0]) > -1)
                {
                    logger.Info("l4-1");

                    var _item =
                    _htmlHtmlElementClass.nextSibling as IHTMLElement;
                    logger.Info(_htmlHtmlElementClass.ToString());
                    logger.Info(_htmlHtmlElementClass.toString());
                    logger.Info("l4-2");
                    res[0] = _item.innerText;
                    //res[0] = _htmlHtmlElementClass.innerText;
                    if (flg)
                        break;
                    flg = true;
                }

                if (_htmlHtmlElementClass.innerText.IndexOf(gOnlineKey[1]) > -1)
                {
                    logger.Info("l5-1");
                    var _item =
                    _htmlHtmlElementClass.nextSibling as IHTMLElement;
                    logger.Info(_htmlHtmlElementClass.ToString());
                    logger.Info(_htmlHtmlElementClass.toString());

                    res[1] = _item.innerText;

                    logger.Info("l5-1");
                    if (flg)
                        break;
                    flg = true;
                }

            }

            return res;
        }

        private HTMLDocument _DomDocment;

#region P1(Fail)
        public bool IsCompleted(int index)
        {
            var _hTMLWindow2Class =
            _DomDocment.frames.item(index) as HTMLWindow2Class;

            return "complete".Equals(_hTMLWindow2Class.document.readyState);
        }

        public CheckResult GetResult()
        {
            var wMenu =
                _DomDocment.frames.item(0) as HTMLWindow2Class;
            var dMenu = wMenu.document as HTMLDocument;
            //dMenu.designMode = "on";
            var wNameCheck =
                _DomDocment.frames.item(1) as HTMLWindow2Class;
            var dNameCheck = wNameCheck.document as HTMLDocument;
            //dNameCheck.designMode = "on";
            

            CheckResult cr = new CheckResult();

            object obj = null;
            //+Menu
            obj = dMenu.getElementById("idName").getAttribute("value");
            if (obj != null)
                cr.idName = obj.ToString();

            obj = dMenu.getElementById("idNo").getAttribute("value");
            if (obj != null)
                cr.idNo = obj.ToString();

            obj = dMenu.getElementById("idAdd").getAttribute("value");
            if (obj != null)
                cr.idAdd = obj.ToString();

            obj = dMenu.getElementById("idTel").getAttribute("value");
            if (obj != null)
                cr.idTel = obj.ToString();

            obj = dMenu.getElementById("idAdd2").getAttribute("value");
            if (obj != null)
                cr.idAdd2 = obj.ToString();

            obj = dMenu.getElementById("idTel2").getAttribute("value");
            if (obj != null)
                cr.idTel2 = obj.ToString();
            //-Menu

            //+NameCheck

            cr.idName0 = dNameCheck.getElementById("idName0").innerText;
            cr.idNo0 = dNameCheck.getElementById("idNo0").innerText;
            cr.idName1 = dNameCheck.getElementById("idName0").innerText;
            cr.idNo1 = dNameCheck.getElementById("idNo0").innerText;
            cr.idName2 = dNameCheck.getElementById("idName0").innerText;
            cr.idNo2 = dNameCheck.getElementById("idNo0").innerText;
            cr.idName3 = dNameCheck.getElementById("idName0").innerText;
            cr.idNo3 = dNameCheck.getElementById("idNo0").innerText;

            cr.idMatch = dNameCheck.getElementById("idMatch").innerText;

            //-NameCheck
            return cr;
        }

        public bool CheckInfoP1(string str)
        
        {
            var _hTMLWindow2Class = 
            this._DomDocment.frames.item(0) as HTMLWindow2Class;

            var _docment =
                _hTMLWindow2Class.document as HTMLDocument;

            _docment.getElementById("idInput").setAttribute("value",str);

            var v1 =
                _hTMLWindow2Class.document.scripts;//??hehe

            string strScript = "fnSelect();";
            foreach (var VARIABLE in v1)
            {
                if((VARIABLE as HTMLScriptElement).src == null)
                {
                    var res =
                         _hTMLWindow2Class.execScript(strScript);
                    return true;
                }

            }
            return false;
        }

#endregion
    }
}
